package br.sed.theenemy.presentation.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import br.sed.theenemy.business.exception.DBException;
import br.sed.theenemy.business.services.Services;
import br.sed.theenemy.model.bean.User;
import br.sed.theenemy.presentation.EnemyConstants;
import br.sed.theenemy.presentation.UserSession;
import br.sed.theenemy.presentation.form.ContactForm;
import br.sed.theenemy.presentation.vo.UserVO;

/**
 * 
 * @author JulioCB
 * @email jcbjulio@gmail.com
 * 
 */
public class ContactAction extends DispatchAction {

    // forwards
    private final String CONTACT_LIST = "ContactList";
    private final String CONTACT_BOX = "ContactBox";
    private final String LOGIN_PAGE = "LoginPage";
    
    public ActionForward unspecified(final ActionMapping mapping,
               final ActionForm form, final HttpServletRequest request, 
               final HttpServletResponse response) {
        ContactForm contactForm = (ContactForm) form;
        
        final UserSession session = new UserSession(request, false);
        final User userLogged = session.getLoggedUser();
        
        if (userLogged == null) {
            // User not logged in, redirect to login.jsp
            return mapping.findForward(LOGIN_PAGE);
        }

        if(!form.validate(mapping, request).isEmpty()){
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        }
        
        List<UserVO> userListVO;
        try {
            userListVO = fillContacts(contactForm, userLogged);
        } catch (DBException e) {
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        }
        
        contactForm.setUserList(userListVO);
        
        return mapping.findForward(CONTACT_LIST);
    }
    
    public ActionForward ContactBox(final ActionMapping mapping,
               final ActionForm form, final HttpServletRequest request, 
               final HttpServletResponse response) {
        ContactForm contactForm = (ContactForm) form;
        
        final UserSession session = new UserSession(request, false);
        final User userLogged = session.getLoggedUser();
         
        if (userLogged == null) {
            // User not logged in, redirect to login.jsp
            return mapping.findForward(LOGIN_PAGE);
        }
        
        if(!form.validate(mapping, request).isEmpty()){
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        }
         
        List<UserVO> userListVO;
        try {
            userListVO = fillContacts(contactForm, userLogged);
        } catch (DBException e) {
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        }
         
        contactForm.setUserList(userListVO);
            
        return mapping.findForward(CONTACT_BOX);
    }
    
    private List<UserVO> fillContacts(ContactForm contactForm, User userLogged) throws DBException{
        Services services = Services.getInstance();
        
        Long sourceId = contactForm.getId();
        int page = contactForm.getPage();
        int max = contactForm.getMax();
        
        if(sourceId == null || sourceId <= 0){
            sourceId = userLogged.getId();
        }
                
        List<User> userList = null;
        if(page==0 || max==0){ 
            userList = services.findAllContactsBySourceId(sourceId);
        }else{
            userList = services.findContactsBySourceId(sourceId, page, max);
        }

        
        List<UserVO> userListVO = new ArrayList<UserVO>();
        for (User user: userList){
            UserVO userVO = new UserVO(user);
            userListVO.add(userVO);
        }
        
        return userListVO;
    }
}
